//
//  CheckCodeController.m
//  FinancialProduct
//  正式用户注册时验证
//  Created by Stella on 2017/9/12.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "CKPuslichTool.h"
#import "PassSetController.h"
#import "UIColor+NN.h"
#import "CheckCodeController.h"

@interface CheckCodeController ()
@property(nonatomic,strong)NSString *myCode;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (weak, nonatomic) IBOutlet UILabel *reminder;
@property (weak, nonatomic) IBOutlet UIButton *NextBt;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation CheckCodeController
- (IBAction)backTop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
-(void)textFieldDidChange:(UITextField *)textField{

    if (textField.text.length>20) {
        textField.text = [textField.text substringToIndex:20];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"新用户注册（2/3）";
    self.reminder.text=@"";
    self.userName.text=self.userInfo[@"name"];
    self.myCode=self.userInfo[@"code"];
    NSString *telNo=self.userInfo[@"phone"];
    NSString *phone=[NSString stringWithFormat:@"%@****%@",[telNo substringToIndex:3],[telNo substringFromIndex:7]];
    
    
    
    NSString *text0=@"您好！您已是我司的客户，验证码已发送到您在本司预留的手机号";
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@，输入并验证完成后设置新密码",text0,phone]];
    UIColor *color=[UIColor colorWithHexString:@"F7EE3E"];
    [aString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(text0.length,11)];
    [self.textLabel setAttributedText:aString];
    
  
    self.CodeTF.layer.cornerRadius=5;
    self.NextBt.layer.cornerRadius=5;
    [self MoveTextfileContent:self.CodeTF];
    [self.CodeTF  addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    // Do any additional setup after loading the view.
}
- (IBAction)checkCode:(id)sender {
    //验证短信验证码，成功后直接去设置密码界面
    
    self.reminder.text=@"";
    
    if (self.CodeTF.text.length == 0) {
        self.reminder.text=@"请输入验证码";
        return;
    }
    if (![self.CodeTF.text isEqualToString:_myCode]) {
        self.reminder.text=@"验证码验证失败";
        return;
    }
    
    NSDate *currentDate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate *localDate1 = [currentDate dateByAddingTimeInterval:interval];
    NSDate *localDate2 = [_oldDate dateByAddingTimeInterval:interval];
    
    //计算时间差间隔
    NSTimeInterval timeBetween = [localDate1 timeIntervalSinceDate:localDate2];
    NSLog(@"timeBetween---%f",timeBetween);
    if (timeBetween>60*10) {
        self.reminder.text=@"验证码已失效";
        return;
    }
   
        //验证码验证成功==》去设置密码
        PassSetController *vc=(PassSetController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PassSetController"];
        vc.isFrome=@"register";
    
        vc.userinfo=self.userInfo;
        [self.navigationController pushViewController:vc animated:YES];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
